package com.zhengtoudai.ui.presenter;

import android.app.Activity;

import com.zhengtoudai.http.HttpApi;
import com.zhengtoudai.injector.PerActivity;
import com.zhengtoudai.ui.bean.ShengTenpay;
import com.zhengtoudai.ui.view.ShengTenpeyView;
import com.zhengtoudai.util.AbToastUtil;

import java.util.Map;

import javax.inject.Inject;

import rx.Subscriber;
import rx.Subscription;
import rx.functions.Action0;

/**
 * 盛付通 通用
 */
@PerActivity
public class ShengTenpayPresenterImpl extends BasePresenterImpl<ShengTenpeyView> implements ShengTenpayPresenter {

    Activity mActivity;
    @Inject
    public ShengTenpayPresenterImpl(Activity activity){
        mActivity=activity;
    }

    @Override
    public void getShengTenpay(String url) {
        Subscription getSubscription=HttpApi.getInstance().getShengTenpay(url).doOnSubscribe(new Action0() {
            @Override
            public void call() {
                mView.showDialog();
            }
        }).subscribe(new Subscriber<ShengTenpay>() {
            @Override
            public void onCompleted() {
                mView.dismissDialog();
            }

            @Override
            public void onError(Throwable e) {
                mView.dismissDialog();
                mView.onFailure(e);
            }

            @Override
            public void onNext(ShengTenpay shengTenpay) {
                if ("200".equals(shengTenpay.getCode())||"成功".equals(shengTenpay.getMessage())){
                    mView.onResult("<!DOCTYPE HTML><html><body>" + shengTenpay.getResult().getParams().getSftReqest() + "</body></html>");
                }else{
                    AbToastUtil.showToast(mActivity,shengTenpay.getMessage());
                }
            }
        });
        addSubscription(getSubscription);
    }

    @Override
    public void postShengTenpay(String url, Map<String, String> params) {
        Subscription postSubscription=HttpApi.getInstance().postShengTenpay(url,params)
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        mView.showDialog();
                    }
                }).subscribe(new Subscriber<ShengTenpay>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissDialog();
                        mView.onFailure(e);
                    }

                    @Override
                    public void onNext(ShengTenpay shengTenpay) {
                        if ("200".equals(shengTenpay.getCode())||"成功".equals(shengTenpay.getMessage())){
                            String html = null;
                            if (shengTenpay.getResult().getParams()!=null){
                                html="<!DOCTYPE HTML><html><body>" + shengTenpay.getResult().getParams().getSftReqest() + "</body></html>";
                                
                            }else if (shengTenpay.getResult().getDataMap()!=null){
                                html="<!DOCTYPE HTML><html><body>" + shengTenpay.getResult().getDataMap().getSftReqest() + "</body></html>";
                            }else if (shengTenpay.getResult().getCallBackParams()!=null){
                                html="<!DOCTYPE HTML><html><body>" + shengTenpay.getResult().getCallBackParams().getSftReqest() + "</body></html>";
                            }
                            mView.onResult(html);
                        }else{
                            AbToastUtil.showToast(mActivity,shengTenpay.getMessage());
                        }
                                         }
                });
        addSubscription(postSubscription);
    }
}
